﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundServiceRef;

    public class OrderTracking : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnGoBack;
        protected DropDownList ddlPriority;
        protected DropDownList ddlStatus;
        protected DropDownList ddlType;
        protected DropDownList ddlWareHouseID;
        protected HtmlForm F7951;
        protected GridView gvOrderTracking;
        protected GridView gvShippedDetail;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnClickIndex;
        protected HtmlInputHidden hdnContainerID;
        protected HtmlInputHidden hdnCopy;
        protected HtmlInputHidden hdnCreatedBy;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnOrderID;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnSelectIndex;
        protected HtmlInputHidden hdnTimeValue;
        protected HtmlHead Head1;
        protected HtmlImage ImgactualShipDate;
        protected HtmlImage imgOwnerID;
        protected Label lblAbnormal;
        protected Label lblActualShipDate;
        protected Label lblCCompany;
        protected Label lblConsigneeID;
        protected Label lblDeliveryDate;
        protected Label lblExternOrderID;
        protected Label lblExternOrderID2;
        protected Label lblHandling;
        protected Label lblIntransitID;
        protected Label lblMsg;
        protected Label lblOperatingDate;
        protected Label lblOperator;
        protected Label lblOrderDate;
        protected Label lblOrderGroup;
        protected Label lblOrderID;
        protected Label lblOrderTracking;
        protected Label lblOwnerID;
        protected Label lblPriority;
        protected Label lblReferenceNum;
        protected Label lblRequestShipDate;
        protected Label lblShippedTotal;
        protected Label lblShippedTotalQty;
        protected Label lblStatus;
        protected Label lblTotalQty;
        protected Label lblTye;
        protected Label lblWareHouseID;
        protected LinkButton lnkbtnDelivery;
        protected LinkButton lnkbtnLost;
        protected LinkButton lnkbtnPartLost;
        protected LinkButton lnkbtnRejected;
        protected LinkButton lnkbtnSettlement;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected Label one3;
        protected ScriptManager ScriptManager1;
        private decimal shippedTotalQty;
        protected HtmlTableCell tdlnkbtnDelivery;
        protected HtmlTableCell tdlnkbtnLost;
        protected HtmlTableCell tdlnkbtnPartLost;
        protected HtmlTableCell tdlnkbtnRejected;
        protected HtmlTableCell tdlnkbtnSettlement;
        protected TextBox txtAbnormal;
        protected TextBox txtActualShipDate;
        protected TextBox txtCCompany;
        protected TextBox txtConsigneeID;
        protected TextBox txtDeliveryDate;
        protected TextBox txtExternOrderID;
        protected TextBox txtExternOrderID2;
        protected TextBox txtHandling;
        protected TextBox txtIntransitID;
        protected TextBox txtOperatingDate;
        protected TextBox txtOperator;
        protected TextBox txtOrderDate;
        protected TextBox txtOrderGroup;
        protected TextBox txtOrderID;
        protected TextBox txtOwnerID;
        protected TextBox txtReferenceNum;
        protected TextBox txtRequestShipDate;
        protected TextBox txtTotalQty;

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("OrderTrackingQuery.aspx?FunctionID=7950", false);
        }

        private decimal GetOrderDetailInfoByGv()
        {
            decimal result = 0M;
            if (this.gvOrderTracking.Rows.Count > 0)
            {
                for (int i = 0; this.gvOrderTracking.Rows.Count > i; i++)
                {
                    result += decimal.Parse(((TextBox) this.gvOrderTracking.Rows[i].FindControl("txtNumberLost")).Text);
                }
            }
            return result;
        }

        protected void gvOrderTracking_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7951", this.gvOrderTracking);
        }

        protected void gvOrderTracking_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
            }
        }

        protected void gvOrderTracking_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                OrdersInfo ordersInfo = base.DataEntityViewState as OrdersInfo;
                DataView dv = CADataConverter.ToDataTable(ordersInfo.OrderDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvOrderTracking, dv);
                base.DataEntityViewState = ordersInfo;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,3);</script>", false);
            }
        }

        protected void gvShippedDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7951", this.gvShippedDetail);
        }

        protected void gvShippedDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as OrdersInfo).PickDetailInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3) </script>", false);
        }

        protected void gvShippedDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvShippedDetail.PageIndex * this.gvShippedDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
                if (((Label) e.Row.FindControl("lblQty")) != null)
                {
                    this.shippedTotalQty += decimal.Parse(((Label) e.Row.FindControl("lblQty")).Text);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblShippedTotalQty.Text = base.GetPreferenceDigit(this.shippedTotalQty);
            }
        }

        protected void gvShippedDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                OrdersInfo ordersInfo = base.DataEntityViewState as OrdersInfo;
                DataView dv = CADataConverter.ToDataTable(ordersInfo.PickDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvShippedDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,3);</script>", false);
            }
        }

        protected void lnkbtnDelivery_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "SHIPPED")
                {
                    this.lblMsg.Text = "只有发货完成的订单,才允许此操作!";
                }
                else
                {
                    List<string> OrderID = new List<string> {
                        this.txtOrderID.Text.Trim()
                    };
                    this.lblMsg.Text = "正常妥投" + OutboundService2ClientProxy.GetInstance().UpdateOrderStatusByOrderID(OrderID, "Delivery", this.txtOperator.Text.Trim(), (this.txtOperatingDate.Text.Trim() == "") ? new DateTime() : DateTime.Parse(this.txtOperatingDate.Text), this.txtAbnormal.Text.Trim(), this.txtHandling.Text.Trim(), new List<string>(), new List<decimal>()) + "状态更新成功!";
                    this.SetShippingOrderInfo2From();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnLost_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "SHIPPED")
                {
                    this.lblMsg.Text = "只有发货完成的订单,才允许此操作!";
                }
                else
                {
                    List<string> OrderID = new List<string> {
                        this.txtOrderID.Text.Trim()
                    };
                    this.lblMsg.Text = "全部丢失" + OutboundService2ClientProxy.GetInstance().UpdateOrderStatusByOrderID(OrderID, "Lost", this.txtOperator.Text.Trim(), (this.txtOperatingDate.Text.Trim() == "") ? new DateTime() : DateTime.Parse(this.txtOperatingDate.Text), this.txtAbnormal.Text.Trim(), this.txtHandling.Text.Trim(), new List<string>(), new List<decimal>()) + "状态更新成功!";
                    this.SetShippingOrderInfo2From();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnPartLost_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "SHIPPED")
                {
                    this.lblMsg.Text = "只有发货完成的订单,才允许此操作!";
                }
                else
                {
                    List<string> OrderID = new List<string> {
                        this.txtOrderID.Text.Trim()
                    };
                    if (this.GetOrderDetailInfoByGv() > 0M)
                    {
                        List<string> lineID = new List<string>();
                        List<decimal> numberLost = new List<decimal>();
                        for (int i = 0; i < this.gvOrderTracking.Rows.Count; i++)
                        {
                            lineID.Add(this.gvOrderTracking.DataKeys[i]["LineID"].ToString());
                            string strNumberLost = ((TextBox) this.gvOrderTracking.Rows[i].FindControl("txtNumberLost")).Text;
                            if (!string.IsNullOrEmpty(strNumberLost))
                            {
                                numberLost.Add(decimal.Parse(strNumberLost));
                            }
                        }
                        this.lblMsg.Text = "部分丢失" + OutboundService2ClientProxy.GetInstance().UpdateOrderStatusByOrderID(OrderID, "PartLost", this.txtOperator.Text.Trim(), (this.txtOperatingDate.Text.Trim() == "") ? new DateTime() : DateTime.Parse(this.txtOperatingDate.Text), this.txtAbnormal.Text.Trim(), this.txtHandling.Text.Trim(), lineID, numberLost) + "状态更新成功!";
                        this.SetShippingOrderInfo2From();
                    }
                    else
                    {
                        this.lblMsg.Text = "没有丢失数量,无法进行状态更新!";
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnRejected_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "SHIPPED")
                {
                    this.lblMsg.Text = "只有发货完成的订单,才允许此操作!";
                }
                else
                {
                    List<string> OrderID = new List<string> {
                        this.txtOrderID.Text.Trim()
                    };
                    this.lblMsg.Text = "顾客拒收" + OutboundService2ClientProxy.GetInstance().UpdateOrderStatusByOrderID(OrderID, "Rejected", this.txtOperator.Text.Trim(), (this.txtOperatingDate.Text.Trim() == "") ? new DateTime() : DateTime.Parse(this.txtOperatingDate.Text), this.txtAbnormal.Text.Trim(), this.txtHandling.Text.Trim(), new List<string>(), new List<decimal>()) + "状态更新成功!";
                    this.SetShippingOrderInfo2From();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnSettlement_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "DELIVERY")
                {
                    this.lblMsg.Text = "只有正常妥投的订单,才允许此操作!";
                }
                else
                {
                    List<string> OrderID = new List<string> {
                        this.txtOrderID.Text.Trim()
                    };
                    this.lblMsg.Text = "货款结算" + OutboundService2ClientProxy.GetInstance().UpdateOrderStatusByOrderID(OrderID, "Settlement", this.txtOperator.Text.Trim(), (this.txtOperatingDate.Text.Trim() == "") ? new DateTime() : DateTime.Parse(this.txtOperatingDate.Text), this.txtAbnormal.Text.Trim(), this.txtHandling.Text.Trim(), new List<string>(), new List<decimal>()) + "状态更新成功!";
                    this.SetShippingOrderInfo2From();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        private void LoadData()
        {
            this.txtOrderDate.Text = base.GetPreferenceShortDate(DateTime.Now);
            this.txtRequestShipDate.Text = base.GetPreferenceShortDate(DateTime.Now);
            this.txtActualShipDate.Text = base.GetPreferenceShortDate(DateTime.Now);
            this.txtDeliveryDate.Text = base.GetPreferenceShortDate(DateTime.Now);
            base.BindData2DDLBySysCode(this.ddlType, "OrderType");
            base.BindData2DDLBySysCode(this.ddlStatus, "OrderStatus");
            base.BindData2DDLBySysCode(this.ddlPriority, "OrderPriority");
            base.BindData2DDL4UserPermissionWareHouse(this.ddlWareHouseID);
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnOrderID.Value = base.Request.QueryString["ID"].ToString();
                this.SetShippingOrderInfo2From();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetShippingOrderInfo2From()
        {
            string OrderID = this.hdnOrderID.Value.ToString();
            OrdersInfo orderInfo = OutboundServiceClientProxy.GetInstance().GetOrdersInfoByOrderID(OrderID);
            base.DataEntityViewState = orderInfo;
            if (orderInfo != null)
            {
                this.txtOrderID.Text = orderInfo.OrderID;
                this.ddlStatus.SelectedValue = orderInfo.Status;
                this.ddlType.SelectedValue = orderInfo.Type;
                this.txtOrderDate.Text = base.GetPreferenceShortDate(orderInfo.OrderDate);
                this.txtOwnerID.Text = orderInfo.OwnerID;
                this.txtOwnerID.Enabled = false;
                this.imgOwnerID.Visible = false;
                this.txtOrderGroup.Text = orderInfo.OrderGroup;
                this.txtReferenceNum.Text = orderInfo.ReferenceNum;
                this.txtRequestShipDate.Text = base.GetPreferenceShortDate(orderInfo.RequestedShipDate);
                this.ddlPriority.SelectedValue = orderInfo.Priority;
                this.txtOperator.Text = orderInfo.Operator;
                this.txtAbnormal.Text = orderInfo.Abnormal;
                this.txtHandling.Text = orderInfo.Handling;
                this.txtOperatingDate.Text = base.GetPreferenceShortDate(DateTime.Now);
                if (!string.IsNullOrEmpty(orderInfo.IntransitID) && (this.ddlWareHouseID.Items.Count > 0))
                {
                    this.ddlWareHouseID.SelectedValue = orderInfo.DestWhDbID;
                }
                this.txtIntransitID.Text = orderInfo.IntransitID;
                this.ddlWareHouseID.Enabled = false;
                this.txtIntransitID.Enabled = false;
                this.txtExternOrderID.Text = orderInfo.ExternOrderID;
                this.txtActualShipDate.Text = string.IsNullOrEmpty(base.GetPreferenceShortDate(orderInfo.ActualShipDate)) ? base.GetPreferenceShortDate(DateTime.Now) : base.GetPreferenceShortDate(orderInfo.ActualShipDate);
                this.txtExternOrderID2.Text = orderInfo.ExternalOrderId2;
                this.txtDeliveryDate.Text = base.GetPreferenceShortDate(orderInfo.DeliveryDate);
                this.txtConsigneeID.Text = orderInfo.ConsigneeID;
                this.txtCCompany.Text = orderInfo.CCompany;
                foreach (OrderDetailInfo info in orderInfo.OrderDetailInfos)
                {
                    info.ReceivedPrice = info.ReceivedQty * Convert.ToDecimal(info.UnitPrice);
                }
                base.BindData2GridView(this.gvShippedDetail, orderInfo.ShippedDetailInfos);
                base.BindData2GridView(this.gvOrderTracking, orderInfo.OrderDetailInfos);
            }
        }
    }
}

